<!-- 编辑客户Modal -->
<div class="modal fade" id="edit-custom" tabindex="-1">
    <div class="modal-dialog">
        <form id="edit_form" name="edit_form" method="post" action="/custom/index/save">
            <input type="hidden" id="id" name="id"/>
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="edit-title">新增客户信息</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-xs-6">
                            <div class="form-group">
                                <label>公司名称<span class="text-important">*</span></label>
                                <input id="name" name="name" class="form-control" type="text" value="">
                            </div>
                        </div>
                        <div class="col-xs-6">
                            <div class="form-group">
                                <label>企业类型<span class="text-important">*</span></label>
                                <div class="dropdown dropdown-block">
                                    <a class="btn btn-default" data-toggle="dropdown"><span id="type_name" name="type_name" class="option-name">企业</span> <span
                                            class="caret"></span>
                                    </a>
                                    <ul class="dropdown-menu dropdown-option-menu">
                                        <volist name="Custom_Type" id="custom_type">
                                            <li><a data-value="{$key}" role="button">{$custom_type}</a></li>
                                        </volist>
                                    </ul>
                                    <input name="type" class="option-value" type="hidden" value="1">
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-12">
                            <div class="form-group">
                                <label>公司地址<span class="text-important">*</span></label>
                                <input id="address" name="address" class="form-control" type="text" value="">
                            </div>
                        </div>
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label>行业<span class="text-important">*</span></label>
                                <div class="dropdown dropdown-block">
                                    <a class="btn btn-default" data-toggle="dropdown"><span name="trade_name" class="option-name">教育</span> <span
                                            class="caret"></span>
                                    </a>
                                    <ul class="dropdown-menu dropdown-option-menu">
                                        <volist name="Custom_Trade" id="custom_trade">
                                            <li><a data-value="{$key}" role="button">{$custom_trade}</a></li>
                                        </volist>
                                    </ul>
                                    <input name="trade" class="option-value" type="hidden" value="1">
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label>微盟目标产品<span class="text-important">*</span></label>
                                <div class="dropdown dropdown-block">
                                    <a class="btn btn-default" data-toggle="dropdown"><span name="target_product_name" class="option-name">请选择</span> <span
                                            class="caret"></span>
                                    </a>
                                    <ul class="dropdown-menu dropdown-option-menu">
                                        <volist name="Custom_Target_Product" id="custom_target_product">
                                            <li><a data-value="{$key}" role="button">{$custom_target_product}</a></li>
                                        </volist>
                                    </ul>
                                    <input name="target_product" class="option-value" type="hidden" value="1">
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label>客户来源</label>
                                <div class="dropdown dropdown-block">
                                    <a class="btn btn-default" data-toggle="dropdown"><span name="source_name" class="option-name">手工创建</span> <span
                                            class="caret"></span>
                                    </a>
                                    <ul class="dropdown-menu dropdown-option-menu">
                                        <volist name="Custom_Source" id="custom_source">
                                            <li><a data-value="{$key}" role="button">{$custom_source}</a></li>
                                        </volist>
                                    </ul>
                                    <input name="source" class="option-value" type="hidden" value="1">
                                </div>
                            </div>
                        </div>
                        <div class="contact-div">
                            <div class="col-xs-12">
                                <div class="form-title">联系人</div>
                            </div>
                            <div class="new-contact">
                                <div class="col-xs-2">
                                    <div class="form-group">
                                        <label>姓名</label>
                                        <a class="deletrow" style="display:none">删除</a>
                                        <input class="form-control" name="contactname[]" type="text">
                                    </div>
                                </div>
                                <div class="col-xs-2">
                                    <div class="form-group">
                                        <label>职位</label>
                                        <input class="form-control" name="contactjob[]" type="text">
                                    </div>
                                </div>
                                <div class="col-xs-3">
                                    <div class="form-group">
                                        <label>手机</label>
                                        <input class="form-control" name="contactphone[]" type="text">
                                    </div>
                                </div>
                                <div class="col-xs-5">
                                    <div class="form-group">
                                        <label>固话</label>
                                        <div class="input-group">
                                            <input class="form-control" name="contactpretel[]" type="text" style="width: 50px; border-right: 0;">
                                            <span class="input-group-addon">-</span>
                                            <input class="form-control" name="contacttel[]" type="text" style="border-left: 0;">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-xs-12">
                                <div class="form-group">
                                    <a id="add-contact-row" class="btn btn-primary"><i class="fa fa-plus"></i></a>
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-12">
                            <div class="form-group">
                                <label>备注</label>
                                <textarea id="remark" name="remark" class="form-control" type="text"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary" id="save_custom">保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('#add-contact-row').click(function () {
            var $this = $(this);
            var $row = $this.parent().parent().prev().clone();
            $row.find('.deletrow').show();
            $row.find('input').val('');
            $this.parent().parent().before($row);
        });

        $(".modal-body").delegate(".deletrow", "click", function () {
            var $this = $(this);
            var $row = $this.parent().parent().parent();
            $row.remove();
        });

        $('a[data-type="new-custom"]').click(function () {
            reset_edit_form();
        });

        $('a[data-type="edit-info"]').click(function () {
            reset_edit_form();
            var $this = $(this);
            var node = $this.parent().parent();
            edit_edit_form(node);
        });

        $("#edit_form").validate({
            submitHandler: function () {
                check_rows();

                $.post($("#edit_form").attr("action"), $("#edit_form").serialize(), function (data) {
                    if (data === "0") {
                        alertify.alert("保存成功!", function () {
                            $('#edit-custom').modal('hide');
                            location.reload();
                        });
                    } else {
                        alertify.alert(data);
                    }
                }).error(function (msg, url, line) {
                    alert(msg.responseText);
                });
            },
            ignore: "",
            rules: {
                name: {
                    required: true
                },
                address: {
                    required: true
                },
//                tel: {
//                    required: true,
//                    isPhone: true
//                },
                found_time: {
                    required: true
                },
            },
            messages: {
                name: {
                    required: "请填写客户名称"
                },
                address: {
                    required: "请填写客户地址"
                },
//                tel: {
//                    required: "请填写客户电话"
//                },
                found_time: {
                    required: "请选择成立日期"
                }
            }
        });

    });

    function check_rows() {
        var msg = '';
        $(".new-contact").each(function () {
            var $this = $(this);
            var name = $this.find('input[name="contactname"]').val();
            var job = $this.find('input[name="contactjob"]').val();
            var phone = $this.find('input[name="contactphone"]').val();
            var tel = $this.find('input[name="contacttel"]').val();
            if (name == '') {
                msg = '联系人名称必填';
            } else if (job == '') {
                msg = '联系人职位必填';
            } else if (phone == '' && tel == '') {
                msg = '联系人手机号或固话至少填一项';
            }
        });

        if (msg != '') {
//            alertify.alert('手机号或固话至少填一项');
            alertify.alert(msg);
        }
    }

    function reset_edit_form() {
        $('#edit-title').text('新增客户信息');
        $('#edit_form input[name="id"]').val('');
        $('#edit_form input[name="name"]').val('');
        $('#edit_form input[name="tel"]').val('');
        $('#edit_form input[name="fax"]').val('');
        init_select($('#edit_form input[name="type"]'), $('#edit_form span[name="type_name"]'), 1);
        init_select($('#edit_form input[name="trade"]'), $('#edit_form span[name="trade_name"]'), 1);
        $('#edit_form input[name="corporate"]').val('');
        init_select($('#edit_form input[name="target_product"]'), $('#edit_form span[name="target_product_name"]'), 1);
        init_select($('#edit_form input[name="source"]'), $('#edit_form span[name="source_name"]'), 1);
        $('#edit_form input[name="found_time"]').val('');
        $('#edit_form input[name="address"]').val('');
        $('#edit_form textarea[name="remark"]').val('');

        $('#edit_form input[name="city"]').citypicker("reset");

        $('.new-contact').each(function (i) {
            if (i > 0) {
                $(this).remove();
            } else {
                $(this).find('input').val('');
            }
        });
        $('.contact-div').show();
    }

    function edit_edit_form(node) {
        $('#edit-title').text('编辑客户信息');
        $('#edit_form input[name="id"]').val(node.data('customid'));
        $('#edit_form input[name="name"]').val(node.data('name'));
        $('#edit_form input[name="tel"]').val(node.data('tel'));
        $('#edit_form input[name="fax"]').val(node.data('fax'));
        init_select($('#edit_form input[name="type"]'), $('#edit_form span[name="type_name"]'), node.data('type'));
        init_select($('#edit_form input[name="trade"]'), $('#edit_form span[name="trade_name"]'), node.data('trade'));
        $('#edit_form input[name="corporate"]').val(node.data('corporate'));
        init_select($('#edit_form input[name="target_product"]'), $('#edit_form span[name="target_product_name"]'), node.data('target_product'));
        init_select($('#edit_form input[name="source"]'), $('#edit_form span[name="source_name"]'), node.data('source'));
        $('#edit_form input[name="found_time"]').val(node.data('found_time'));
        $('#edit_form input[name="address"]').val(node.data('address'));
        $('#edit_form textarea[name="remark"]').val(node.data('remark'));

        try {
            var citystr = node.data('city');
            var cityarr = citystr.split("/");
            $('#edit_form input[name="city"]').citypicker("reset");
            $('#edit_form input[name="city"]').citypicker("destroy");
            $('#edit_form input[name="city"]').citypicker({
                province: cityarr[0],
                city: cityarr[1],
                district: cityarr[2]
            });
        } catch (e) {
        }
        $('.contact-div').hide();
    }

    function init_select(ctr, ctr_name, data) {
        ctr.val(data);
        var vx = ctr.val();
//        if (vx != "") {
            ctr.prev().find('a').each(function () {
                var $this = $(this);
                if ($this.data('value') == vx) {
                    ctr_name.text($this.text());
                }
            });
//        }
    }
</script>